package com.simple.datax.transform.tcs;

import net.easipay.ec.v2.ECDataType;
import net.easipay.ec.v2.ECFlow201Document;
import net.easipay.ec.v2.ECFlow201Document.ECFlow201;
import net.easipay.ec.v2.ECFlowType;
import net.easipay.ec.v2.ECFlowType.ActionList;
import net.easipay.ec.v2.ECFlowType.ActionList.ActionId;
import net.easipay.ec.v2.ECLinkManType;
import net.easipay.ec.v2.ECLinkManType.ECLinkMan;
import net.easipay.ec.v2.ECUserType;

import org.apache.xmlbeans.XmlObject;

import cn.gov.chinaport.tcs.flow.TcsFlow201Document;
import cn.gov.chinaport.tcs.flow.TcsFlowType;
import cn.gov.chinaport.tcs.flow.TcsLinkManType;
import cn.gov.chinaport.tcs.flow.TcsLinkManType.TcsLinkMan;
import cn.gov.chinaport.tcs.flow.TcsUserType;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.simple.datax.api.TransformerException;
import com.simple.datax.transform.util.TransformUtils;

/**
 * @Title: Tcs2EcFlowTransformer.java
 * @Package com.simple.datax.transform.tcs
 * @Description: TcsFlow TO EcFlow
 * @author Paladin Chen
 * @date 2013-1-9 下午4:07:41
 * @version V1.0
 */
public class Tcs2EcFlowTransformer {
	private static final Logger logger = LoggerFactory.getLogger(Tcs2EcFlowTransformer.class);

	public ECFlow201Document transform(TcsFlow201Document xmlDoc) throws TransformerException {
		ECFlow201Document ecflowdoc = ECFlow201Document.Factory.newInstance();
		ECFlow201 ecflow = ecflowdoc.addNewECFlow201();

		//
		TcsFlowType tcsflowT = xmlDoc.getTcsFlow201().getTcsFlow();
		if (tcsflowT != null) {
			ECFlowType flowType = ecflow.addNewECFlow();
			flowType.setBpNo(TransformUtils.changeNullToBlank(tcsflowT.getBpNo()));

			flowType.setCorpTaskId(TransformUtils.changeNullToBlank(tcsflowT.getCorpTaskId()));

			String taskid = TransformUtils.changeNullToBlank(tcsflowT.getTaskId());
			// 如果原始报文中的messageid为null，则使用taskid填充
			if (tcsflowT.getMessageId() != null) {
				flowType.setMessageId(tcsflowT.getMessageId());
			} else {
				flowType.setMessageId(taskid);
			}

			flowType.setTaskControl(TransformUtils.changeNullToBlank(tcsflowT.getTaskControl()));

			flowType.setTaskId(taskid);

			flowType.setTaskNote(TransformUtils.changeNullToBlank(tcsflowT.getTaskNote()));

			cn.gov.chinaport.tcs.flow.TcsFlowType.ActionList ecal = tcsflowT.getActionList();
			if (ecal != null) {
				ActionList actionList = flowType.addNewActionList();

				for (String acid : ecal.getActionIdArray()) {
					ActionId actionId = actionList.addNewActionId();
					actionId.setStringValue(TransformUtils.changeNullToBlank(acid));
				}
			}
		}

		// 用户类型
		TcsUserType tcsuser = xmlDoc.getTcsFlow201().getTcsUser();
		if (tcsuser != null) {
			ECUserType ecuser = ecflow.addNewECUser();
			ecuser.setUserId(TransformUtils.changeNullToBlank(tcsuser.getUserId()));

			ecuser.setUserPrivateKey(TransformUtils.changeNullToBlank(tcsuser.getUserPrivateKey()));
		}

		// 联系人
		TcsLinkManType tcsul = xmlDoc.getTcsFlow201().getTcsLink();
		if (tcsul != null) {
			TcsLinkMan[] ecmen = tcsul.getTcsLinkManArray();

			if (ecmen != null && ecmen.length > 0) {
				ECLinkManType eclink = ecflow.addNewECLink();
				for (TcsLinkMan tcslinkman : tcsul.getTcsLinkManArray()) {
					ECLinkMan eclinkman = eclink.addNewECLinkMan();

					eclinkman.setAddress(TransformUtils.changeNullToBlank(tcslinkman.getAddress()));
					eclinkman.setDepartment(TransformUtils.changeNullToBlank(tcslinkman.getDepartment()));
					eclinkman.setDuty(TransformUtils.changeNullToBlank(tcslinkman.getDuty()));
					eclinkman.setEmail(TransformUtils.changeNullToBlank(tcslinkman.getEmail()));
					eclinkman.setFax(TransformUtils.changeNullToBlank(tcslinkman.getFax()));
					eclinkman.setImCode(TransformUtils.changeNullToBlank(tcslinkman.getImCode()));
					eclinkman.setImType(TransformUtils.changeNullToBlank(tcslinkman.getImType()));
					eclinkman.setMobile(TransformUtils.changeNullToBlank(tcslinkman.getMobile()));
					eclinkman.setName(TransformUtils.changeNullToBlank(tcslinkman.getName()));
					eclinkman.setTelephone(TransformUtils.changeNullToBlank(tcslinkman.getTelephone()));
					eclinkman.setZipCode(TransformUtils.changeNullToBlank(tcslinkman.getZipCode()));
				}
			}
		}

		// 报文内容
		if (xmlDoc.getTcsFlow201().getTcsData() != null) {
			String srcData = xmlDoc.getTcsFlow201().getTcsData().xmlText();
			// String srcDataType;

			try {
				// 对于Tcs的回复Flow201 data，目前没有需要特别转换

				// XmlObject catdoc2 = XmlObject.Factory.parse(srcData);
				// srcDataType =
				// catdoc2.getDomNode().getFirstChild().getNodeName();
				// logger.debug(srcDataType);
				//
				// // 目前只对报关单进行转换
				// if ("DeclarationDocument".equalsIgnoreCase(srcDataType)) {
				// srcData = Ec2TcsDecTransformer.transform(srcData);
				// }

				ECDataType ecDataType = ecflow.addNewECData();
				ecDataType.set(XmlObject.Factory.parse(srcData));
				logger.debug("Mapping的ES报文：" + srcData);
			} catch (Exception e) {
				logger.error("Cannot parse flow data; Now will ignore this error.");
				throw new TransformerException("Parse Flow data error.", e);
			}
		}
		return ecflowdoc;
	}
}
